package com.jxtti.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxtti.dao.ArticleCategoryDao;
import com.jxtti.entity.ArticleCategory;

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class ArticleCategoryService {
	private final static Logger log= Logger.getLogger(ArticleCategoryService.class);	

	@Autowired
    private ArticleCategoryDao<ArticleCategory> articleCategoryDao;
	
	
	public List<ArticleCategory>  queryAll(Map<String, Object> data){
		return  articleCategoryDao.queryByList(data);
	}	
		
	public List<ArticleCategory>  findByPid(Map<String, Object> data){
		return  articleCategoryDao.findByPid(data);
	}
	
	public List<ArticleCategory>  findPidAndPid(Map<String, Object> data){
		return  articleCategoryDao.findPidAndPid(data);
	}
	
	public List<ArticleCategory>  findChildAndChild(Map<String, Object> data){
		return  articleCategoryDao.findChildAndChild(data);
	}
	
	
	// 前台分页
	public PageInfo<ArticleCategory> listPage(Map<String, Object> data) {
		// 第几页
		int pageNum = Integer.parseInt((data.get("currPageNum") == null || data
				.get("currPageNum").equals("0")) ? "1" : data
				.get("currPageNum").toString());
		// 每页显示条数 
		int pageSize = Integer
				.parseInt((data.get("pageRowSize") == null || data.get(
						"pageRowSize").equals("0")) ? "10" : data.get(
						"pageRowSize").toString());

		PageHelper.startPage(pageNum, pageSize);
		PageInfo<ArticleCategory> pages = new PageInfo<ArticleCategory>(
				articleCategoryDao.queryByList(data));
		return pages;
	}
	
	
	/**
	 * 返回easyui数结构的组织架构
	 * 
	 * @param data
	 * @return
	 */
	public List<Map<String, Object>> getEasyUiTree(Map<String, String> data) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<ArticleCategory> list=new ArrayList<ArticleCategory>(); 
		
		list.addAll(articleCategoryDao.getTreeNodes(data));
		
		if (null != list && 0 != list.size()) { //判断是否有数据
			for (int i = 0; i < list.size(); i++) { //循环数据				
							
				Map<String, Object> node = new HashMap<String, Object>();					
				node.put("id", list.get(i).getId()); //是 getCode	
				node.put("parentID", list.get(i).getParentID());
				node.put("code", list.get(i).getCode());
				node.put("name", list.get(i).getName());		
				node.put("levelID", list.get(i).getLevelID());
				node.put("url", list.get(i).getUrl());
				node.put("sortID", list.get(i).getSortID());									
				node.put("visible", list.get(i).getVisible());		
				node.put("fk_orgId", list.get(i).getFk_orgId());
				node.put("orgIdName", list.get(i).getOrgIdName());				
				node.put("is_contribute", list.get(i).getIs_contribute());
						
				if ("true".equals(list.get(i).getIsParent())) { //是父级节点
					// 区域节点，需要展开下级
					List<Map<String, Object>> childResult = new ArrayList<Map<String, Object>>();
					
					Map<String, String> childData=data; //传递 type 和status
					System.out.println(list.get(i).getId());
					childData.put("parentID", list.get(i).getId().toString());//参数 父类 改变了						
					childData.put("isRoot",null);//	
					
					childResult=getEasyUiTree(childData);//---------------递归 			
		
					node.put("children", childResult);
				}				
				result.add(node);
			}
		}
		return result;
	}
	
	
	public List<Map<String, Object>> getEasyUiTreeForSelect(Map<String, String> data) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		List<ArticleCategory> list=new ArrayList<ArticleCategory>(); 
		
		list.addAll(articleCategoryDao.getTreeNodes(data));		
		if (null != list && 0 != list.size()) { //判断是否有数据	
			for (int i = 0; i < list.size(); i++) { //循环数据
				Map<String, Object> node = new HashMap<String, Object>();					
				node.put("id", list.get(i).getId());				
				node.put("text", list.get(i).getName());	
				
//				if ("true".equals(list.get(i).getIsParent())) { //是父级节点
//					node.put("state","closed");	 //异步加载	 		
//				}else{
//					node.put("state","open");		
//				}				
				
				// 自定义属性
				    Map<String, Object> attrNode = new HashMap<String, Object>();							
					attrNode.put("levelID", list.get(i).getLevelID());//级别	
					attrNode.put("fk_orgId", list.get(i).getFk_orgId());//所属科室					
					node.put("attributes",attrNode);
				if ("true".equals(list.get(i).getIsParent())) { //是父级节点
					// 区域节点，需要展开下级
					List<Map<String, Object>> childResult = new ArrayList<Map<String, Object>>();					
					Map<String, String> childData=data; //传递 type 和status					
					childData.put("parentID", list.get(i).getId().toString());//参数 父类 改变了	
					childData.put("isRoot",null);//	
					
					childResult=getEasyUiTreeForSelect(childData);//---------------递归 				
					node.put("children", childResult);
				}				
				result.add(node);
			}
		}
		return result;
	}
	
	@Autowired
	private ArticleListService articleListService;
	
	/**
	 * 返回 z-tree 区域 站点 树结构
	 * 
	 * @param data
	 * @return
	 */
	public List<ArticleCategory> getzTreeNodes(Map<String, String> data) {					
		return articleCategoryDao.getTreeNodes(data);
	}
	
	
	public int add(ArticleCategory tCityAQIWrite)
	{
		int insertCount = articleCategoryDao.add(tCityAQIWrite);
		return insertCount;
	}	

	
	public int update(ArticleCategory tCityAQIWrite)
	{
		int updateCount = articleCategoryDao.update(tCityAQIWrite);
		return updateCount;
	}	

	
	public int delete(ArticleCategory entity)
	{
		int deleteCount = articleCategoryDao.delete(entity);		
		return deleteCount;
	}
	
	
	public ArticleCategory queryById(ArticleCategory entity)
	{
		entity = articleCategoryDao.queryById(entity);
		return entity;
	}

}
